package controller

import (
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/topaz-h/gin-web/params"
	"github.com/topaz-h/gin-web/service"
	"github.com/topaz-h/gin-web/tools"
)

// AuthController 结构体
type AuthController struct {
}

// Router 路由
func (a *AuthController) Router(engine *gin.Engine) {
	// 获取阿里云短信验证码
	engine.GET("/alipay/sendsmscode", a.sendSmsCode)
	// 短信验证码实现登录
	engine.POST("/login_sms", a.loginSms)
	// 获取图形验证码
	engine.GET("/captcha", a.captcha)
	// 图形验证码自我验证
	engine.POST("/verify_captcha", a.verifyCaptcha)
	// 图形验证码实现登录
	engine.POST("/login_captcha", a.loginCaptcha)
	// 上传头像 folder+id+file
	engine.POST("/upload_avatar", tools.UploadFile(), a.updateAvatar)
}

// SendSmsCode MVC 解析"/alipay/sendcode"
func (a *AuthController) sendSmsCode(c *gin.Context) {
	phone, exist := c.GetQuery("phone")
	if !exist {
		tools.Failed(c, "参数手机号解析失败")
		return
	}
	// 实例化Service
	as := service.AuthService{}
	isSend := as.SendSmsCode(phone)
	// isSend := true
	if isSend {
		tools.Success(c, "发送成功")
		return
	}
	tools.Failed(c, "发送失败")
}

// LoginSms 手机验证码登录、没注册顺便注册
func (a *AuthController) loginSms(c *gin.Context) {
	var loginSmsParams params.LoginSmsParams
	err := tools.Decode(c.Request.Body, &loginSmsParams)
	if err != nil {
		tools.Failed(c, "参数解析失败")
		return
	}
	// 完成手机+验证码登录
	as := service.AuthService{}
	member := as.LoginSms(loginSmsParams)
	// 返回指针类型才能与nil比较，值类型不行
	if member != nil {
		// 登陆成功
		tools.Success(c, member)
		return
	}
	tools.Failed(c, "登录失败")
}

// captcha 借助第三方库生产图形验证码
func (a *AuthController) captcha(c *gin.Context) {
	tools.GenerateCaptcha(c)
}

// 验证图形验证码是否正确
func (a *AuthController) verifyCaptcha(c *gin.Context) {
	var captchaResult tools.CaptchaResult
	err := tools.Decode(c.Request.Body, &captchaResult)
	if err != nil {
		tools.Failed(c, "验证码参数解析失败")
		return
	}
	result := tools.VerifyCaptcha(captchaResult.Id, captchaResult.VerifyValue)
	if result {
		tools.Success(c, "验证成功")
	} else {
		tools.Failed(c, "验证失败")
	}
}

// loginCaptcha 通过`验证码+密码+验证码登录`
func (a *AuthController) loginCaptcha(c *gin.Context) {
	// 1. 解析参数
	var loginParams params.LoginCaptchaParams
	err := tools.Decode(c.Request.Body, &loginParams)
	if err != nil {
		tools.Failed(c, "验证码参数解析失败")
		return
	}
	// 2. 验证验证码
	validate := tools.VerifyCaptcha(loginParams.Id, loginParams.Code)
	if !validate {
		tools.Failed(c, "验证码错误，请重新验证！")
		return
	}
	// 3. 验证用户表
	as := service.AuthService{}
	member := as.LoginCaptcha(loginParams.Username, loginParams.Password)
	if member.Id != 0 {
		// 登陆成功
		tools.Success(c, &member)
		return
	}
	tools.Failed(c, "登陆失败！")
}

// 保存路径到用户表
func (a *AuthController) updateAvatar(c *gin.Context) {
	id := c.Request.PostFormValue("id")
	idInt64, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		tools.Failed(c, "id转int64失败")
		return
	}
	fileFullPath := c.MustGet("fileFullPath").(string)

	// 4. 保存到相应数据库中
	as := service.AuthService{}
	path := as.UploadAvatar(idInt64, fileFullPath)
	if path != "" {
		config := tools.GetConfig()
		c.Set("path", config.AppProtocol+"://"+config.AppHost+":"+config.AppPort+path)
		return
	}
	tools.Failed(c, "路径保存至数据库失败")
}
